<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\{
    Master as MasterModel,
    MasterCate,
    MasterVideo,
    MasterVideoGoods
};
class Master extends Common{
    //大师分类模型
    protected $master_cate;
    //大师模型
    protected $master;
    //大师视频模型
    protected $master_video;
    //视频推荐商品模型
    protected $video_goods;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->master = new MasterModel();
        $this->master_cate=new MasterCate();
        $this->master_video = new MasterVideo();
        $this->video_goods = new MasterVideoGoods();
    }
    /**
     *大师分类列表
     * return json
     */
  public function getMasterCateList(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $list=$this->master_cate->select();
      if(!empty($list)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['list']=$list;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
  /**
   *获取分类信息
   * return json
   */
  public function getCateInfo(){
     if(empty($this->adminInfo)){
         $msg['message']=checkMsg(LOGIN_ERROR);
         return $msg;
     }
     $cate_id = $this->request->post('cate_id');
     if(empty($cate_id)){
        $msg['message']=checkMsg(CANSHU_ERROR);
        return $msg;
     }
     $info = $this->master_cate->where(['cate_id'=>$cate_id])->find();
     if(empty($info)){
         $msg['message']=checkMsg(GET_DATA_FAILE);
     }else{
         $msg['message']=checkMsg(GET_DATA_SUCCESS);
         $msg['info']=$info;
     }
     return $msg;
  }
  /**
   *添加或修改大师分类
   *return json
   */
  public function addOrEditeCate(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $cate_id = $this->request->post('cate_id');
      $cate_name =$this->request->post('cate_name');
      $cate_status=$this->request->post('cate_status',1);
      if(empty($cate_name)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      if(empty($cate_id)){  //添加分类
          $count = $this->master_cate->where(['cate_name'=>$cate_name])->count();
          if($count>0){
              $msg['message']=checkMsg(CATE_IS_EXITE);
              return $msg;
          }
          $data=[
              'cate_name' => $cate_name,
              'cate_status' =>$cate_status,
              'add_time'    =>time()
          ];
          $res=$this->master_cate->insertGetId($data);
      }else{  //修改分类
          $data=[
              'cate_name' => $cate_name,
              'cate_status' =>$cate_status,
          ];
          $res=$this->master_cate->save($data,['cate_id'=>$cate_id]);
      }
      if($res){
          $msg['message']=checkMsg(OPERA_SUCCESS);
      }else{
          $msg['message']=checkMsg(OPERA_FAIL);
      }
     return $msg;
  }
  /**
   *修改大师分类状态
   * return json
   */
  public function editeCateStatus(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $cate_id = $this->request->post('cate_id');
      $status  = $this->request->post('status',1);
      if(empty($cate_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $res=$this->master_cate->save(['cate_status'=>$status],['cate_id'=>$cate_id]);
      if($res){
          $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
      }else{
           $msg['message']=checkMsg(UPDATE_DATA_FAIL);
      }
      return $msg;
  }
  /**
   *删除大师分类
   * return json
   */
  public function deleteMaterCate(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $cate_id=$this->request->post('cate_id');
      if(empty($cate_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $count=$this->master->where(['cate_id'=>$cate_id,'is_delete'=>0])->count();
      if($count>0){
          $msg['message']=checkMsg(THE_CATE_HAVE_CONTENT);
          return $msg;
      }
      $res=$this->master_cate->where(['cate_id'=>$cate_id])->delete();
      if($res){
          $msg['message']=checkMsg(OPERA_SUCCESS);
      }else{
          $msg['message']=checkMsg(OPERA_FAIL);
      }
      return $msg;
  }
  /**
   *大师列表
   * return json
   */
  public function getMasterList(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $pageNo   = $this->request->post('pageNo',1);
      $pageSize = $this->request->post('pageSize',10);
      $cate_id  = $this->request->post('cate_id');
      $keywords = $this->request->post('keywords','');
      $where['is_delete']=0;
      if(!empty($cate_id)){
          $where['cate_id']=$cate_id;
      }
      if(!empty($keywords)){
          $where['m_name']=['like',"%$keywords%"];
      }
      $list = $this->master->where($where)->page($pageNo,$pageSize)->order('m_id','desc')->select();
      $total= $this->master->where($where)->count();
     if(!empty($list)){
         $msg['message']=checkMsg(GET_DATA_SUCCESS);
         $msg['list']=$list;
         $msg['total']=$total;
     }else{
         $msg['message']=checkMsg(GET_DATA_FAILE);
     }
     return $msg;
  }
  /**
   *所有大师列表
   * return json
   */
  public function getAllMaster(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $list = $this->master->where(['is_delete'=>0])->field('m_id,m_name')->select();
      if(empty($list)){
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }else{
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['list']=$list;
      }
      return $msg;
  }
  /**
   *获取大师信息
   * return json
   */
  public function getMasterInfo(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $master_id = $this->request->post('master_id');
      if(empty($master_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $info=$this->master->where(['m_id'=>$master_id,'is_delete'=>0])->find();
      if(empty($info)){
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }else{
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['info'] =$info;
      }
      return $msg;
  }
  /**
   *添加或修改大师信息
   * return json
   */
  public function addOrEditeMater(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $input=$this->request->post();
      $check_error=$this->validate($input,'Master');
      if($check_error!==true){
          $msg=checkMsg($check_error);
          return ['message'=>$msg];
      }
      if(isset($input['m_id']) && !empty($input['m_id'])){ //修改
           $data=[
               'm_head_img'  =>$input['m_head_img'],
               'm_name'  =>$input['m_name'],
               'm_phone'  =>$input['m_phone'],
               'm_intro'  =>$input['m_intro'],
               'cate_id'  =>$input['cate_id'],
               'm_posters'  =>$input['m_posters'],
               'update_time' =>time()
           ];
           $res=$this->master->save($data,['m_id'=>$input['m_id']]);
      }else{  //添加
          $data=[
              'm_head_img'  =>$input['m_head_img'],
              'm_name'  =>$input['m_name'],
              'm_phone'  =>$input['m_phone'],
              'm_intro'  =>$input['m_intro'],
              'cate_id'  =>$input['cate_id'],
              'm_posters'  =>$input['m_posters'],
              'add_time' =>time()
          ];
          $res=$this->master->insertGetId($data);
      }
      if($res){
          $msg['message']=checkMsg(OPERA_SUCCESS);
      }else{
          $msg['message']=checkMsg(OPERA_FAIL);
      }
      return $msg;
  }
  /**
   *删除大师
   * return json
   */
  public function deleteMaster(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $m_id=$this->request->post('m_id');
      if(empty($m_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $count=$this->master->where(['m_id'=>$m_id])->count();
      if($count<1){
          $msg['message']=checkMsg(MASTER_NOT_EXIT);
          return $msg;
      }
      $res=$this->master->save(['is_delete'=>1],['m_id'=>$m_id]);
      if($res){
          $this->master_video->save(['is_delete'=>1],['m_id'=>$m_id]);
          $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(DELETE_DATA_FAIL);
      }
      return $msg;
  }
    /**
     *大师视频列表
     * return json
     */
    public function getVideoList(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $pageNo = $this->request->post('pageNo',1);
        $pageSize=$this->request->post('pageSize',10);
        $m_id = $this->request->post('m_id');
        $keywords = $this->request->post('keywords');
        $where=[];
        if(!empty($m_id)){
            $where['mv.m_id']=$m_id;
        }
        if(!empty($keywords)){
            $where['mv.v_name']=['like',"%$keywords%"];
        }
        $list=$this->master_video->alias('mv')
            ->join('yc_master m','m.m_id=mv.m_id','left')
            ->where($where)
            ->field('m.m_name,mv.*')
            ->page($pageNo,$pageSize)
            ->order('mv.v_id','desc')
            ->select();
        $total =$this->master_video->alias('mv')
            ->join('yc_master m','m.m_id=mv.m_id','left')
            ->where($where)
            ->field('m.m_name,mv.*')
            ->order('mv.v_id','desc')
            ->count();
        if(empty($list)){
            $msg['message']=checkMsg(GET_DATA_FAILE);
        }else{
            $msg['message']=checkMsg(GET_DATA_SUCCESS);
            $msg['list']=$list;
            $msg['total']=$total;
        }
        return $msg;
    }
    /**
     *获取大师视频信息
     * return json
     */
    public function getVideoInfo(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $mv_id = $this->request->post('mv_id');
        if(empty($mv_id)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $info=$this->master_video->alias('mv')
                                 ->join('yc_master m','m.m_id=mv.m_id','left')
                                 ->where(['mv.is_delete'=>0,'mv.v_id'=>$mv_id])
                                 ->field('m.m_id,mv.v_id,mv.v_name,mv.v_href,mv.v_desc,mv.status,mv.v_thumb')
                                 ->find();
        if(empty($info)){
            $msg['message']=checkMsg(GET_DATA_FAILE);
        }else{
            $msg['message']=checkMsg(GET_DATA_SUCCESS);
            $msg['info']=$info;
        }
        return $msg;
    }
  /**
   *添加或修改大师视频
   * return json
   */
 public function addOrEditeVideo(){
     if(empty($this->adminInfo)){
         $msg['message']=checkMsg(LOGIN_ERROR);
         return $msg;
     }
     $m_id  = $this->request->post('m_id');
     $v_id  = $this->request->post('v_id');
     $v_name= $this->request->post('v_name');
     $v_desc= $this->request->post('v_desc');
     $v_thumb =$this->request->post('v_thumb');
     $video   =$this->request->file('video');
     if(empty($v_name) ||empty($v_thumb) || empty($video)){
         $msg['message']=checkMsg(CANSHU_ERROR);
         return $msg;
     }
     if(is_string($video)){
         $v_url=$video;
     }else{
         $res=$this->uploadVideo($video);
         if($res['code']==0){
             $msg['message']=checkMsg(UPLOAD_VIDEO_FAIL);
             return $msg;
         }else{
             $v_url=$res['video_url'];
         }
     }
   if(isset($v_id)&&!empty($v_id)){  //修改视频信息
      $data=[
          'v_name' =>$v_name,
          'v_href'  =>$v_url,
          'v_desc' =>$v_desc,
          'v_thumb'=>$v_thumb,
          'update_time'=>time()
      ];
      $res=$this->master_video->save($data,['v_id'=>$v_id]);
   }else{  //添加
       if(empty($m_id)){
           $msg['message']=checkMsg(CANSHU_ERROR);
           return $msg;
       }
       $data=[
           'm_id'   =>$m_id,
           'v_name' =>$v_name,
           'v_href'  =>$v_url,
           'v_desc' =>$v_desc,
           'v_thumb'=>$v_thumb,
           'add_time'=>time()
       ];
       $res=$this->master_video->insertGetId($data);
   }
   if($res){
       $msg['message']=checkMsg(OPERA_SUCCESS);
   }else{
       $msg['message']=checkMsg(OPERA_FAIL);
   }
   return $msg;
 }
 /**
 *删除大师视频
 *return json
 */
 public function deleteVideo(){
    if(empty($this->adminInfo)){
        $msg['message']=checkMsg(LOGIN_ERROR);
        return $msg;
    }
    $v_id = $this->request->post('v_id');
    if(empty($v_id)){
        $msg['message']=checkMsg(CANSHU_ERROR);
        return $msg;
    }
    $res=$this->master_video->save(['is_delete'=>1],['v_id'=>$v_id]);
    if($res){
        $this->video_goods->where(['vid'=>$v_id])->delete();
        $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
    }else{
        $msg['message']=checkMsg(DELETE_DATA_FAIL);
    }
    return $msg;
}
  /**
   *修改大师视频状态
   * return json
   */
  public function editeVideoStatus(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $v_id = $this->request->post('v_id');
      $status = $this->request->post('status',1);
      if(empty($v_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $res = $this->master_video->save(['status'=>$status],['v_id'=>$v_id]);
      if($res){
          $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(UPDATE_DATA_FAIL);
      }
      return $msg;
  }
 /**
  *大师视频推荐商品列表
  *return json
  */
  public function getVideoGoodsList(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $pageNo   = $this->request->post('pageNo',1);
      $pageSize = $this->request->post('pageSize',10);
      $m_id     = $this->request->post('m_id');
      $keywords = $this->request->post('keywords');
      $where=[];
      if(!empty($m_id)){
          $where['m.m_id']=$m_id;
      }
      if(!empty($keywords)){
          $where['g.goods_name']=['like',"%$keywords%"];
      }
      $list = $this->video_goods->alias('vg')
                                ->join('yc_master_video mv','vg.vid=mv.v_id','left')
                                ->join('yc_master m','m.m_id=mv.m_id','left')
                                ->join('yc_goods g','g.goods_id=vg.gid','left')
                                ->field('vg.id,m.m_name,mv.v_name,g.goods_name,vg.is_hot')
                                ->where($where)
                                ->page($pageNo,$pageSize)
                                ->order('id','desc')
                                ->select();
      $total = $this->video_goods->alias('vg')
          ->join('yc_master_video mv','vg.vid=mv.v_id','left')
          ->join('yc_master m','m.m_id=mv.m_id','left')
          ->join('yc_goods g','g.goods_id=vg.gid','left')
          ->field('vg.id,m.m_name,mv.v_name,g.goods_name,vg.is_hot')
          ->where($where)
          ->order('id','desc')
          ->count();
       if(empty($list)){
             $msg['message']=checkMsg(GET_DATA_FAILE);
       }else{
           $msg['message']=checkMsg(GET_DATA_SUCCESS);
           $msg['list']=$list;
           $msg['total']=$total;
       }
       return $msg;
  }
  /**
   *大师推荐商品信息
   * return json
   */
  public function getVideoGoodsInfo(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $id=$this->request->post('id');
      if(empty($id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $info=$this->video_goods->alias('vg')
                               ->join('yc_master_video mv','mv.v_id=vg.vid','left')
                               ->join('yc_master m','m.m_id=mv.m_id','left')
                               ->join('yc_goods g','g.goods_id=vg.gid','left')
                               ->join('yc_goods_cate gc','gc.cate_id=g.goods_cate','left')
                               ->where(['vg.id'=>$id])
                               ->field('vg.id,m.m_id,m.m_name,mv.v_id,mv.v_name,gc.cate_id,vg.gid as goods_id,g.goods_name,gc.cate_name')
                               ->find();
      if(empty($info)){
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }else{
           $msg['message']=checkMsg(GET_DATA_SUCCESS);
           $msg['info']=$info;
      }
     return $msg;
  }
  /**
   *添加或修改视频推荐商品
   *return json
   */
  public function addOrEditeVideoGoods(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $vg_id = $this->request->post('vg_id');
      $gid = $this->request->post('goods_id');
      if(empty($vg_id)){  //添加
          $mid = $this->request->post('m_id');
          $vid = $this->request->post('v_id');
          if(empty($mid) || empty($vid) || empty($gid)){
              $msg['message']=checkMsg(CANSHU_ERROR);
              return $msg;
          }
          $data=[
              'mid'  =>$mid,
              'vid'  =>$vid,
              'gid'  =>$gid
          ];
         $count=$this->video_goods->where($data)->count();
         if($count>0){
             $msg['message']=checkMsg(CANNOT_RE_ADD);
             return $msg;
         }
         $data['add_time']=time();
         $res=$this->video_goods->insertGetId($data);
      }else{  //修改
           if(empty($goods_id)){
               $msg['message']=checkMsg(CANSHU_ERROR);
               return $msg;
           }
           $res=$this->video_goods->save(['gid'=>$gid],['id'=>$vg_id]);
      }
      if($res){
          $msg['message']=checkMsg(OPERA_SUCCESS);
      }else{
          $msg['message']=checkMsg(OPERA_FAIL);
      }
      return $msg;
  }
  /**
   *删除视频推荐商品
   * return json
   */
  public function deleteVideoGoods(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $vg_id = $this->request->post('vg_id');
      if(empty($vg_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $res=$this->video_goods->where(['id'=>$vg_id])->delete();
      if($res){
          $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(DELETE_DATA_FAIL);
      }
      return $msg;
  }
  /**
   *修改视频推荐商品的热卖状态
   * return json
   */
  public function editeVideoGoodsHot(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $id = $this->request->post('id');
      $is_hot = $this->request->post('is_hot',1);
      if(empty($id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $res= $this->video_goods->save(['is_hot'=>$is_hot],['id'=>$id]);
      if($res){
          $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(UPDATE_DATA_FAIL);
      }
      return $msg;
  }
}